package com.tt.mymap;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class LocationActivity extends Activity {
    private TextView text;  //定义用于显示LocationProvider的TextView组件

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置窗口全屏显示
        text = (TextView) findViewById(R.id.location);

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);// 获取系统的定位服务
        // 检查是否已经授予了定位权限
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // 如果没有权限，申请权限
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
            }, 1);
            return;
        }

        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,
                1000,
                1,
                new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        locationUpdates(LocationActivity.this, location); // 传递 Activity 上下文
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                    }
                });

        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        locationUpdates(this, location);
    }

    public void locationUpdates(Context context, Location location) {
        if (location != null) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("您的位置信息：\n");
            stringBuilder.append("经度：").append(location.getLongitude()).append("\n");
            stringBuilder.append("纬度：").append(location.getLatitude()).append("\n");
            stringBuilder.append("时间：");

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            stringBuilder.append(dateFormat.format(new Date(location.getTime())));

            Geocoder geocoder = new Geocoder(context, Locale.getDefault());// 创建Geocoder对象用于地理编码
            try {
                List<android.location.Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                if (addresses != null && !addresses.isEmpty()) {
                    android.location.Address address = addresses.get(0);
                    stringBuilder.append("\n地址：").append(address.getAddressLine(0));// 添加地址信息到字符串中
                } else {
                    stringBuilder.append("\n地址：未找到");
                }
            } catch (Exception e) {
                e.printStackTrace();
                stringBuilder.append("\n地址：无法获取");
            }

            text.setText(stringBuilder);
        } else {
            text.setText("没有获取到GPS信息");
        }
    }
}


